<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="app-content-list">
      <div class="layui-form-item">
        <div class="layui-inline">
          <label class="layui-form-label">搜索</label>
          <div class="layui-input-inline">
            <input type="text" name="keyword" placeholder="请输入关键词" autocomplete="off" class="layui-input">
          </div>
        </div>

        <div class="layui-inline">
          <label class="layui-form-label">所属分类</label>
          <div class="layui-input-inline">
            <script type="text/html" template lay-url="/api/adverts/getCategoryList"
              lay-done="layui.form.render('select');">
                <select name="category_id" id="category_id" required>
                     <option value="">请选择</option>
                      {{# layui.each(d.data,function(index,item){ }}
                     <option value="{{ item.id }}" {{ item.name === d.params.category_name ? 'selected' : '' }}>{{ item.name }}</option>
                      {{# }); }}
                </select>
              </script>
          </div>
        </div>
        <div class="layui-inline">
          <button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="LAY-app-contlist-search">
            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
          </button>

          <button class="layui-btn layuiadmin-btn-list" data-type="add">添加</button>
        </div>
      </div>
    </div>

    <div class="layui-card-body">
      <table id="LAY-app-content-list" lay-filter="LAY-app-content-list"></table>
        <script type="text/html" id="switc_status">
          <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="显示|隐藏" lay-filter="switch" {{(d.status=='1')?'checked' : '' }}>
        </script>
      <script type="text/html" id="table-content-list">
          <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="show">预览</a>
          <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">编辑</a>
          <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
        </script>
    </div>
  </div>
</div>

<script>
  layui.use('adverts', layui.factory('adverts')).use(['admin', 'adverts', 'table', 'form', 'layedit'], function () {
    var $ = layui.$
      , admin = layui.admin
      , view = layui.view
      , table = layui.table
      , layedit = layui.layedit
      , form = layui.form;
    
    form.render(null, 'app-content-list');

    //监听搜索
    form.on('submit(LAY-app-contlist-search)', function (data) {
      var field = data.field;

      console.log(field);

      //执行重载
      table.reload('LAY-app-content-list', {
        where: field
      });
    });

    var active = {
      //添加
      add: function (othis) {
        admin.popup({
          title: '上传相册'
          , area: ['1000px', '800px']
          , id: 'LAY-popup-content-add'
          , success: function (layero, index) {
            view(this.id).render('/advert/add').done(function () {

              form.render(null, 'layuiadmin-app-form-list');

              //监听提交
              form.on('submit(layuiadmin-app-form-submit)', function (data) {
                var field = data.field; //获取提交的字段
                
                field.lang = lang

                if (field.status == 'on') {
                  field.status = 1;
                } else {
                  field.status = 0;
                }
                //提交 Ajax 成功后，关闭当前弹层并重载表格
                admin.req({
                  type: "POST",
                  url: '/api/adverts/add',
                  data: { data: field },
                  dataType: "json",
                  success: function (res) {
                    layer.msg(res.msg, { time: 800 });

                    layui.table.reload('LAY-app-content-list'); //重载表格
                    layer.close(index); //执行关闭
                  }
                });
              });
            });
          }
        });
      }
    };

    $('.layui-btn.layuiadmin-btn-list').on('click', function () {
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });

  });
</script>